package com.suep.business.controller;

import com.suep.business.domain.Meal;
import com.suep.business.domain.MealCategory;
import com.suep.business.domain.Store;
import com.suep.business.service.IMealCategoryService;
import com.suep.business.service.IMealService;
import com.suep.business.service.IStoreService;
import com.suep.system.config.BaseR;
import com.suep.system.utils.RequestAttributeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/mealCategory")
public class MealCategoryController {
    @Autowired
    private IMealCategoryService mealCategoryService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IMealService mealService;

    /**
     * 获取所有菜品分类
     */
    @GetMapping
    public BaseR<List<MealCategory>> getAll() {
        return new BaseR<>().ok("获取所有分类列表", mealCategoryService.getAll());
    }

    /**
     * 根据ID获取菜品分类
     */
    @GetMapping("/{categoryId}")
    public MealCategory getById(@PathVariable Integer categoryId) {
        return mealCategoryService.getById(categoryId);
    }


    @GetMapping("/getByStoreId")
    public BaseR<List<MealCategory>> getByStoreId(@RequestParam Integer storeId) {
       List<MealCategory> mealCategoryList=mealCategoryService.getByStoreId(storeId);
        return new BaseR<>().ok("获取该商店分类列表",mealCategoryList);
    }
    /**
     * 根据userID二次查询获取菜品分类，适用于商家
     */
    @GetMapping("/getByUserId")
    public BaseR<List<MealCategory>> getByUserId(HttpServletRequest request) {
        //查到用户的商店ID
        Integer userId= RequestAttributeUtil.getUserIdFromRequest(request);
        Store store=storeService.getStoreByUserId(userId);
        return new BaseR<>().ok("获取所有分类列表",mealCategoryService.getByStoreId(store.getStoreId()));
    }

    /**
     * 添加菜品分类
     */
    @PostMapping
    public BaseR<Integer> add(@RequestBody MealCategory mealCategory,HttpServletRequest request) {
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        Integer storeId=storeService.getStoreByUserId(userId).getStoreId();
        mealCategory.setStoreId(storeId);
        return new BaseR<>().ok("新增分类成功",mealCategoryService.add(mealCategory));
    }

    /**
     * 更新菜品分类
     */
    @PutMapping
    public BaseR<Integer> update(@RequestBody MealCategory mealCategory) {
        return new BaseR<>().ok("更新菜品标签成功",mealCategoryService.update(mealCategory));
    }

    /**
     * 根据ID删除菜品分类,注意级联删除
     */
    @DeleteMapping
    public BaseR<Integer> deleteById(@RequestParam("categoryId") Integer categoryId,HttpServletRequest request) {
       Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
       Integer storeId=storeService.getStoreByUserId(userId).getStoreId();
       List<Meal> mealList=mealService.selectMealsByStoreId(storeId);
        for(Meal meal:mealList){
            if (Objects.equals(meal.getCategoryId(), categoryId)){
                //说明此时不能级联删除
                return new BaseR<>().error("此标签还在菜品在使用，无法删除");
            }
        }
        return new BaseR<>().ok("菜品标签删除成功",mealCategoryService.deleteById(categoryId));
    }

    /**
     * 根据店铺ID删除菜品分类
     */
    @DeleteMapping("/deleteByStoreId/{storeId}")
    public int deleteByStoreId(@PathVariable Integer storeId) {
        return mealCategoryService.deleteByStoreId(storeId);
    }
}
